package com.assistant.admin.controller;
import com.assistant.admin.pojo.Login;
import com.assistant.admin.pojo.PageResult;
import com.assistant.admin.pojo.PageVariable;
import com.assistant.admin.pojo.SpManager;
import com.assistant.admin.service.SpManagerService;
import com.assistant.common.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class SpManagerController {
    @Autowired
    private SpManagerService spManagerService;

    /*
     * 添加一个管理用户
     * */
    @PostMapping("/addUser")
    public JSONResult insertSpManager(@RequestBody SpManager spManager) {

        int i = spManagerService.insertSpManager(spManager);
        if (i == 1) {
            return JSONResult.ok("注册成功");
        } else if (i == -1) {
            return JSONResult.errorMsg("用户名已注册");
        }
        return JSONResult.errorMsg("注册失败");
    }

    /*
     * 修改密码
     * */
    @PutMapping("/user/{id}")
    public int updatePassword(@PathVariable("id") Integer id, @RequestBody SpManager spManager) {
        return spManagerService.updatePassword(id, spManager);
    }

    /*
     * 删除用户
     * */
    @DeleteMapping("/user/{id}")
    public int deleteUser(@PathVariable("id") Integer id) {
        return spManagerService.deleteSpManager(id);
    }

    /*
     * 修改用户状态
     * */
    @PutMapping("/userStatus/{id}/{status}")
    public int updateStatus(@PathVariable("id") Integer id, @PathVariable("status") Integer status) {
        return spManagerService.updateStatus(id, status);
    }

    /*
     * 登录
     * */
    @PostMapping("/loginUser")
    public JSONResult login(@RequestBody Login login) {
        List<SpManager> loginSpManager = spManagerService.isCheckLogin(login);
        if (!loginSpManager.isEmpty()) {
            return JSONResult.ok(loginSpManager);
        } else {
            return JSONResult.errorMsg("登录失败！");
        }
    }

    /*
     * 设置用户权限
     * */
    @PutMapping("/updateRootAuthority/{id}/{rootAuthority}")
    public int updateRootAuthority(@PathVariable("id") Integer id, @PathVariable("rootAuthority") Integer rootAuthority) {
        return spManagerService.updateRootAuthority(id, rootAuthority);
    }

    /*
     * 查询所有列表
     * */
    @PostMapping("/getUserList")
    public PageResult<SpManager> getUserList(@RequestBody PageVariable pageVariable) {
        return spManagerService.getListSpManager(pageVariable.getPage(), pageVariable.getRows(), pageVariable.getKey());
    }

    /*
     * 根据id查询一条记录的值
     * */
    @GetMapping("/getUser/{id}")
    public SpManager getUser(@PathVariable("id") Integer id) {
        return spManagerService.selectSpManager(id);
    }
}
